/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.wib.dao.impl;

import edu.upc.wib.dao.ContendorDao;
import edu.upc.wib.entity.Contendor;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author dangeles
 */
@Repository
public class ContendorDaoImpl extends SimpleJdbcDaoSupport implements ContendorDao {

    private static Logger log = LoggerFactory.getLogger(ContendorDaoImpl.class);

    @Autowired
    public ContendorDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    
    
    @Override
    public Integer crear(Contendor contendor) {
        getJdbcTemplate().update(
                "insert into Contendor (idTorneo,idContendor, nombre, texto, rutaImagen, agrupadorInicial) "
                + "values (?, ?, ?, ?, ?, ?)",
                contendor.getIdTorneo(),contendor.getIdContendor(),contendor.getNombre(), contendor.getTexto(), 
                contendor.getRutaImagen(),contendor.getAgrupadorInicial());
        
        // esto no aplica cuando no es autoincrement
        // return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
        return contendor.getIdContendor();
    }

    @Override
    public void actualizar(Contendor contendor) {
        getJdbcTemplate().update(
                "update Contendor set nombre=?, texto=?, rutaImagen=?, agrupadorInicial=? " +
                "where idTorneo = ? and idContendor = ?",
                contendor.getNombre(), contendor.getTexto(), 
                contendor.getRutaImagen(),contendor.getAgrupadorInicial(), contendor.getIdTorneo(), 
                contendor.getIdContendor());
    }

    @Override
    public Contendor obtener(Integer idTorneo, Integer idContendor) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select * from Contendor where idTorneo = ? and idContendor = ?",
                    new BeanPropertyRowMapper<Contendor>(Contendor.class), idTorneo, idContendor);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Contendor> obtenerTodos() {
        return getSimpleJdbcTemplate().query(
                "select * from Contendor",
                new BeanPropertyRowMapper<Contendor>(Contendor.class));
    }

    @Override
    public void eliminar(Integer id) {
        getJdbcTemplate().update(
                "delete from Contendor where idContendor = ?", id);
    }

    public List<Contendor> obtenerContendorPorTorneo(Integer idTorneo) {
       try {
            return getSimpleJdbcTemplate().query(
                    "select * from Contendor where idTorneo = ?",
                    new BeanPropertyRowMapper<Contendor>(Contendor.class), idTorneo);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
